<script setup>
import { ref, onMounted } from 'vue'
import { sectWeek } from '@/api/insertweek' // 假设有一个获取请假记录的接口
import { useUserStore } from '@/stores/user'
import { ElMessageBox, ElMessage, ElDialog } from 'element-plus'

const userStore = useUserStore()
const tableData = ref([])
const showReasonDialog = ref(false)
const currentRecordId = ref(null)
const currentReason = ref('')

// 状态文本和颜色映射
const statusTextMap = {
  1: '待审批',
  2: '已批准',
  3: '已驳回',
}

const statusColorMap = {
  1: '#E6A23C', // 黄色（待审批）
  2: '#67C23A', // 绿色（已批准）
  3: '#F56C6C', // 红色（已拒绝）
}

const getStatusText = (status) => {
  return statusTextMap[status] || '未知状态'
}

const getStatusColor = (status) => {
  return statusColorMap[status] || '#303133' // 默认黑色
}

// 显示驳回理由对话框
const showReason = (record) => {
  currentRecordId.value = record.id
  currentReason.value = record.reason || '无理由'
  showReasonDialog.value = true
}

// 获取请假记录数据
const fetchLeaveRecords = async () => {
  try {
    const res = await sectWeek({
      userId: userStore.user.id // 根据用户 ID 获取请假记录
    })
    if (res && res.data) {
      tableData.value = res.data.data 
      showReason.vHtml  = res.data.data
    } else {
      tableData.value = []
    }
  } catch (error) {
    console.error('获取请假记录失败:', error)
    tableData.value = []
  }
}

// 组件挂载时获取数据
onMounted(() => {
  fetchLeaveRecords()
})
</script>

<template>
  <el-card class="info-card">
    <el-button type="primary" class="info-button">
      我的请假记录
    </el-button>
  </el-card>

  <el-card class="table-card" style="margin-top: 20px;">
    <el-table :data="tableData" style="width: 100%" height="600">
      <!-- 序号 -->
      <el-table-column type="index" label="序号" width="80" align="center" />

      <!-- 名字 -->
      <el-table-column prop="userId" label="工号" width="120" align="center" />

      <!-- 请假类型 -->
      <el-table-column prop="typeDate" label="请假类型" width="120" align="center">
        <template #default="scope">
          <span :style="{ color: getStatusColor(scope.row.typeDate) }">
            {{ scope.row.typeDate === 1 ? '事假' :
                  scope.row.typeDate === 2 ? '病假' :
                  scope.row.typeDate === 3 ? '年假' :
                  scope.row.typeDate === 4 ? '婚假' :
                  scope.row.typeDate === 5 ? '产假' : '未知类型' }}
          </span>
        </template>
      </el-table-column>

      <!-- 开始时间 -->
      <el-table-column prop="startTime" label="开始时间" width="150" align="center" />

      <!-- 创建时间 -->
      <el-table-column prop="creatTime" label="创建时间" width="150" align="center" />

      <!-- 结束时间 -->
      <el-table-column prop="endTime" label="结束时间" width="150" align="center" />

      <!-- 状态 -->
      <el-table-column prop="status" label="状态" width="100" align="center">
        <template #default="scope">
          <span :style="{ color: getStatusColor(scope.row.status) }">
            {{ getStatusText(scope.row.status) }}
          </span>
        </template>
      </el-table-column>

      <!-- 内容 -->
      <el-table-column prop="content" label="内容" align="center">
        <template #default="scope">
          <div v-html="scope.row.content"></div>
        </template>
      </el-table-column>

      <!-- 操作列 -->
      <el-table-column label="操作" prop="remark" width="150" align="center">
        <template #default="scope">
          <el-tooltip placement="top">
            <template #content>
              <span>{{ scope.row.remark ? scope.row.remark : '无理由' }}</span>
            </template>
            <el-button>标注</el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
  </el-card>

  <!-- 查看驳回理由对话框 -->
  
</template>

<style scoped>
.info-card {
  height: 70px;
  border: none;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 20px;
}

.info-button {
  width: 200px;
  font-size: 16px;
  border-radius: 8px;
  text-align: center;
  background: linear-gradient(to right, #4a6bff, #7986cb);
  border: none;
  transition: all 0.3s;
}

.info-button:hover {
  background: linear-gradient(to right, #7986cb, #4a6bff);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.info-button:active {
  transform: translateY(0);
}

.table-card {
  border: none;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 10px;
  padding: 20px;
}
</style>